package sunstar.com.xq.adapter

import android.app.ActionBar
import android.app.Activity
import android.app.WallpaperManager
import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.ColorDrawable
import android.support.v4.view.PagerAdapter
import android.support.v4.view.ViewPager
import android.view.Gravity
import android.view.View
import org.jetbrains.anko.toast
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.ListPopupWindow
import android.widget.PopupWindow
import com.squareup.picasso.Picasso
import org.jetbrains.anko.AnkoLogger
import sunstar.com.xq.R
import sunstar.com.xq.activity.ImageCheckActivity
import sunstar.com.xq.util.ScreenUtil
import sunstar.com.xq.util.UtilBox

/**
 * Created by Sun.Star on 2017/6/15.
 */
class ImageViewPagerAdapter(var context: Context,var stringList : MutableList<String>) : PagerAdapter(),AnkoLogger {
    override fun isViewFromObject(view: View?, `object`: Any?): Boolean  = view == `object`

    override fun getCount(): Int  = stringList.size
    override fun instantiateItem(container: ViewGroup?, position: Int): Any {
        var image = ImageView(context)
        image.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT)
        Picasso.with(context).load(stringList[position]).into(image)
        container!!.addView(image)
        image.setOnLongClickListener {
            showPopWindow(ScreenUtil.getVagueBackground(context as Activity,image),image)
            true
        }
        image.setOnClickListener { (context as ImageCheckActivity).finish() }
        return image
    }
    fun showPopWindow(bitmap:Bitmap,view:View){
        var popView = View.inflate(context, R.layout.vague_pop_layout,null)
        var window = PopupWindow(context)
        popView.layoutParams = ViewGroup.LayoutParams(UtilBox.getWindowHeight(context as Activity),ViewGroup.LayoutParams.MATCH_PARENT)
        window.contentView = popView
        var color = ColorDrawable()
        color.color = context.resources.getColor(R.color.nocolor)
        window.setBackgroundDrawable(color)
        window.width = ViewGroup.LayoutParams.MATCH_PARENT
        window.height= ViewGroup.LayoutParams.MATCH_PARENT
        (popView.findViewById(R.id.back_img) as ImageView ).setImageBitmap(bitmap)
        (popView.findViewById(R.id.back_img) as ImageView ).setOnClickListener {
            window.dismiss()
        }
        popView.findViewById(R.id.set_text).setOnClickListener {
            WallpaperManager.getInstance(context).setBitmap((((view as ImageView).drawable) as BitmapDrawable).bitmap)
            window.dismiss()
        }
        window.showAtLocation(view,Gravity.CENTER,0,0)

    }
    override fun destroyItem(container: ViewGroup?, position: Int, `object`: Any?) {
        container!!.removeView(`object` as ImageView)
    }
}